package cn.yshujia.controller;

import cn.yshujia.annotation.DecryptFields;
import cn.yshujia.domain.entity.Role;
import cn.yshujia.domain.vo.ApiVO;
import cn.yshujia.service.impl.RoleServiceImpl;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * @author YShuJia
 * @create 2024/4/23
 * @description 控制器
 */

@RestController
@Tag (name = "Role", description = "角色Api")
public class RoleController {
	
	@Resource
	private RoleServiceImpl service;
	
	@GetMapping ("/admin/role/list")
	@DecryptFields
	@Operation (summary = "admin 获取所有角色")
	@PreAuthorize ("@sys.hasOnePermission('role:all')")
	public ApiVO<List<Role>> page() {
		return ApiVO.success(service.list());
	}
	
	@GetMapping ("/admin/role/permission/map")
	@Operation (summary = "admin 获取角色权限 map")
	@PreAuthorize ("@sys.hasOnePermission('role:all')")
	public ApiVO<Map<String, List<String>>> mapPermission() {
		return ApiVO.success(service.mapPermission());
	}
	
	@PutMapping ("/admin/role/update")
	@Operation (summary = "admin 修改角色")
	@PreAuthorize ("@sys.hasOnePermission('role:all')")
	public ApiVO<Boolean> update(@RequestBody Role role) {
		Boolean result = service.update(role);
		if (result) {
			return ApiVO.success(true);
		}
		return ApiVO.error();
	}
}
